package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.dao.BannerDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.Banner;
import org.springframework.stereotype.Repository;

@Repository
public class BannerDaoImpl extends AbstractDaoImpl<Banner, Integer> implements
		BannerDao {

	protected BannerDaoImpl() {
		super(Banner.class);
	}

	@Override
	public void saveBanner(Banner banner) {
		saveOrUpdate(banner);
	}

	@Override
	public List<Banner> findBanner(String positionCode) {
		return findByCriteria(Restrictions.like("positionCode", positionCode,
				MatchMode.START));
	}

	@Override
	public List<Banner> searchBanner(Date fromDate, Date toDate) {
		Criteria criteria = getCurrentSession().createCriteria(Banner.class);

		if (fromDate != null) {
			criteria.add(Restrictions.ge("startDate", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("endDate", toDate));
		}

		return findByCriteria(criteria);
	}

	@Override
	public Page<Banner> searchBanner(Date fromDate, Date toDate,
			GridPramDTO gridPramDTO) {
		
		Criteria criteria = getCurrentSession().createCriteria(Banner.class);

		if (fromDate != null) {
			criteria.add(Restrictions.ge("startDate", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("endDate", toDate));
		}

		return findByCriteria(criteria, gridPramDTO);
	}

}
